//
//  SettingViewController.m
//  JZRJAPP
//
//  Created by mac on 16/6/1.
//  Copyright © 2016年 mac. All rights reserved.
//

#import "SettingViewController.h"
#import "NicknameController.h"
#import "FeedBackController.h"
@interface SettingViewController ()<UITableViewDataSource,UITableViewDelegate,UINavigationControllerDelegate,UIImagePickerControllerDelegate,TwoViewControllerDelegate>

@property (nonatomic,strong) UIView *topView;
@property (nonatomic,strong)UILabel *titleLabel;
@property (nonatomic,strong)UITableView *tableView;

@property (nonatomic,strong)UIImageView *imageView;
@property (nonatomic,strong)UILabel *nameLabel;
@end

static NSString *indentfiler = @"cellfiler";
@implementation SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.topView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 64)];
    _topView.backgroundColor =[UIColor redColor];
    
    UIButton *leftBtn =[UIButton buttonWithType:UIButtonTypeCustom];
    
    leftBtn.frame =CGRectMake(5, 15, 50, 50);
    [leftBtn setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    
    //[leftBtn setTitle:@"返回" forState:UIControlStateNormal];
    [leftBtn addTarget:self action:@selector(backClick:) forControlEvents:UIControlEventTouchUpInside];

    self.titleLabel =[[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 2 -50, 20, 100, 40)];
    self.titleLabel.text = @"我的";
    self.titleLabel.font = [UIFont systemFontOfSize:20];
    self.titleLabel.textAlignment = NSTextAlignmentCenter;
    self.titleLabel.textColor = [UIColor whiteColor];
    
    self.tableView =[[UITableView alloc]initWithFrame:CGRectMake(0, 64, SCREEN_WIDTH, SCREEN_HEIGHT) style:UITableViewStyleGrouped];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    
    [self.tableView registerClass:[UITableViewCell class]  forCellReuseIdentifier:indentfiler];
    
    [self.topView addSubview:leftBtn];
    [self.topView addSubview:self.titleLabel];
    [self.view addSubview:self.topView];
    
    [self.view addSubview:self.tableView];

}

-(void)backClick:(UIButton *)sender{
    [self dismissViewControllerAnimated:YES completion:nil];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - Table view data source

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return 5;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:indentfiler forIndexPath:indexPath];
    if (indexPath.section == 0) {
        cell.textLabel.text = @"头像";
        UIImageView *imageView =[[UIImageView alloc]init];
        
        imageView.frame = CGRectMake(SCREEN_WIDTH / 4 *3, 5, 40, 40);
        //切圆角
        imageView.layer.cornerRadius = 20;
        imageView.layer.masksToBounds = YES;
        imageView.userInteractionEnabled = YES;
        self.imageView = imageView;
        [imageView setImage:[UIImage imageNamed:@"aa"]];
        [cell.contentView addSubview:imageView];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 1) {
        cell.textLabel.text = @"昵称";
        
        UILabel *nameLabel = [[UILabel alloc]initWithFrame:CGRectMake(SCREEN_WIDTH / 3 *2 , 10, SCREEN_WIDTH / 4, 30)];
        nameLabel.backgroundColor =[UIColor greenColor];
        nameLabel.text = @"abcd";
        self.nameLabel = nameLabel;
        [cell.contentView addSubview:nameLabel];
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 2) {
        cell.textLabel.text = @"意见反馈";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 3) {
        cell.textLabel.text = @"关于";
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator; //显示最右边的箭头
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
    }
    if (indexPath.section == 4) {
        
        UIButton *quitBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        
        quitBtn.frame = CGRectMake(SCREEN_WIDTH/ 2 -100, 10, 200, 30);
        
        [quitBtn setTitle:@"退出" forState:UIControlStateNormal];
        
        quitBtn.backgroundColor = [UIColor redColor];
        quitBtn.layer.cornerRadius = 10;
        
        [quitBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        
        [cell.contentView addSubview:quitBtn];
    }
   
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 5;
}
-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 50;
}



-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        /**
         *  弹出提示框
         */
        //初始化提示框
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:nil preferredStyle:UIAlertControllerStyleActionSheet];
        //按钮：从相册选择，类型：UIAlertActionStyleDefault
        [alert addAction:[UIAlertAction actionWithTitle:@"从相册选择" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            //初始化UIImagePickerController
            UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
            //获取方式1：通过相册（呈现全部相册），UIImagePickerControllerSourceTypePhotoLibrary
            //获取方式2，通过相机，UIImagePickerControllerSourceTypeCamera
            //获取方法3，通过相册（呈现全部图片），UIImagePickerControllerSourceTypeSavedPhotosAlbum
            PickerImage.sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
            //允许编辑，即放大裁剪
            PickerImage.allowsEditing = YES;
            //自代理
            PickerImage.delegate = self;
            //页面跳转
            [self presentViewController:PickerImage animated:YES completion:nil];
        }]];
        //按钮：拍照，类型：UIAlertActionStyleDefault
        [alert addAction:[UIAlertAction actionWithTitle:@"拍照" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action){
            /**
             其实和从相册选择一样，只是获取方式不同，前面是通过相册，而现在，我们要通过相机的方式
             */
            UIImagePickerController *PickerImage = [[UIImagePickerController alloc]init];
            //获取方式:通过相机
            PickerImage.sourceType = UIImagePickerControllerSourceTypeCamera;
            PickerImage.allowsEditing = YES;
            PickerImage.delegate = self;
            [self presentViewController:PickerImage animated:YES completion:nil];
        }]];
        //按钮：取消，类型：UIAlertActionStyleCancel
        [alert addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        [self presentViewController:alert animated:YES completion:nil];
    }
    
    if (indexPath.section == 1) {
        
        NicknameController *nickNameVC =[[NicknameController alloc]init];
        nickNameVC.name =self.nameLabel.text;
        nickNameVC.delegate =self;
        [self presentViewController:nickNameVC animated:YES completion:nil];
        NSLog(@"修改昵称");
    }
    if (indexPath.section == 2) {
        FeedBackController *feedBackVC =[[FeedBackController alloc]init];
        [self presentViewController:feedBackVC animated:YES completion:nil];
    }
}

//PickerImage完成后的代理方法
- (void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    //定义一个newPhoto，用来存放我们选择的图片。
    UIImage *newPhoto = [info objectForKey:@"UIImagePickerControllerEditedImage"];
    self.imageView.image = newPhoto;
    
    NSLog(@"%@",newPhoto);
    
    [self dismissViewControllerAnimated:YES completion:nil];
}

//代理反向传值
-(void)inputString:(NSString*)textStr{
    self.nameLabel.text = textStr;
}

@end
